package com.edu.hre.struts.action.leave;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreLeaveRequest;
import com.edu.hre.db.pojo.HreLeaveType;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.leave.LeaveApproveListForm;
import com.edu.hre.struts.form.leave.LeaveSummaryListForm;
import com.edu.hre.struts.form.leave.LeaveSummaryUpdateForm;

public class LeaveSummaryListAction extends DispatchAction {
	
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-yyyy");
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation()); 
		LeaveSummaryListForm beanForm = (LeaveSummaryListForm) form;
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			beanForm.setAsc(!beanForm.isAsc());
			CommonUtils.sortCollection(beanForm.getListObjs(), beanForm.isAsc(), sortBy);
			return mapping.findForward("leaveSummary");
		}
		if(beanForm.getFromDate() == null){
			beanForm.setFromDate(dateFormat.parse(dateFormat.format(new Date())));
		}
		if(beanForm.getToDate() == null){
			beanForm.setToDate(new Date());
		}
		String columns[] = new String[]{"firstLeave", "lastLeave"};
		int compares[] = new int[]{Compare.GREATER_EQUAL, Compare.LESS_EQUAL};
		Object[] values = new Object[]{beanForm.getFromDate(), beanForm.getToDate()};
		if("true".equalsIgnoreCase(request.getParameter("filter"))){
			int empNum = Integer.parseInt(request.getParameter("empNum"));
			columns = new String[]{"firstLeave", "lastLeave", "empNum"};
			compares = new int[]{Compare.GREATER_EQUAL, Compare.LESS_EQUAL, Compare.EQUAL};
			values = new Object[]{beanForm.getFromDate(), beanForm.getToDate(), empNum};
		}
		List<HreLeaveType> listLeaveType = baseService.getPojos(null, -1, null, null, null, 0, 0, HreLeaveType.class);
		List<LeaveSummaryUpdateForm> listObjs = new ArrayList<LeaveSummaryUpdateForm>();
		List<HreLeaveRequest> listLeaveRequest = baseService.getPojosAnd(columns, compares, values, 
				new String[]{"firstLeave"}, new Boolean[]{true}, pageSize, (currentPage - 1) * pageSize, HreLeaveRequest.class);
		Float totalRowsLeaved = 0f;
		for(int i = 0; listLeaveRequest != null && i < listLeaveRequest.size(); i++){
			try {
				HreLeaveRequest leaveRequest = listLeaveRequest.get(i);
				HreEmployee employee = baseService.getPojo(leaveRequest.getEmpNum(), HreEmployee.class);
				HreEmployee supervisor = baseService.getPojo(leaveRequest.getSupervisorNum(), HreEmployee.class);
				String empFullName = CommonUtils.getFullName(employee);
				String supFullName = CommonUtils.getFullName(supervisor);
				String userStr = employee.getMainUsername() == null ? "" : employee.getMainUsername();
				LeaveSummaryUpdateForm obj = new LeaveSummaryUpdateForm(leaveRequest.getId(), leaveRequest.getEmpNum(), empFullName, 
						userStr, leaveRequest.getFirstLeave(), leaveRequest.getLastLeave(), leaveRequest.getTotalLeaved(), 
						leaveRequest.getSupervisorNum(), supFullName, leaveRequest.getSupComment(), 
						leaveRequest.getApproved(), leaveRequest.getLeaveTypeId(), getNameLeaveType(listLeaveType, leaveRequest.getLeaveTypeId()));
				listObjs.add(obj);
				totalRowsLeaved += Float.parseFloat(obj.getTotalLeavedThisTime());
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		beanForm.setTotalLeaved(totalRowsLeaved);
		beanForm.setListObjs(listObjs);
		int totalItems = baseService.countByColumns(columns, compares, values, "id", HreLeaveRequest.class);
		ArrayList<HrePageInfo> allPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(totalItems));
		beanForm.setTotalPage(CommonUtils.getTotalPage(totalItems));
		beanForm.setListPages(allPages);
		beanForm.setTotalItems(totalItems);  
		
		return mapping.findForward("leaveSummary");
	}
	
	private String getNameLeaveType(List <HreLeaveType> listObjs, int id){
		for(int i = 0; listObjs != null && i < listObjs.size(); i++){
			HreLeaveType tmp = listObjs.get(i);
			if(id == tmp.getId()){
				return tmp.getName();
			}
		}
		return null;
	}
}
